/* ===================

AX2E (AXESCON XACML 2.0 Engine) is the Java authorization engine, which implements OASIS XACML 2.0 standard.
Copyright (C) 2007 AXESCON LLC

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


Contact: AXESCON LLC - info{at}axescon.com

 =================== */
package axescon.xacml.xb.ctx;

import axescon.xacml.model.policy.AttributeDesignator;
import axescon.xacml.model.policy.PolicyObject;
import axescon.xacml.model.policy.SubjectAttributeDesignator;
import axescon.xacml.model.policy.impl.ActionAttributeDesignatorImpl;
import axescon.xacml.model.policy.impl.EnvironmentAttributeDesignatorImpl;
import axescon.xacml.model.policy.impl.ResourceAttributeDesignatorImpl;
import axescon.xacml.model.policy.impl.SubjectAttributeDesignatorImpl;
import x0PolicySchemaOs.oasisNamesTcXacml2.AttributeDesignatorType;
import x0PolicySchemaOs.oasisNamesTcXacml2.SubjectAttributeDesignatorType;

/**
 * @author Date: Sep 30, 2005
 *         Responsibilities:
 */
public class XBAttributeDesignatorFactory {

    public SubjectAttributeDesignator getSubjectAttributeDesignator(SubjectAttributeDesignatorType xbat
                                                                    , PolicyObject parent) throws Exception {
        if (xbat == null) return null;

        SubjectAttributeDesignatorImpl ret = new SubjectAttributeDesignatorImpl();
        getAttributeDesignator(ret, xbat, parent);
        if (xbat.isSetSubjectCategory()) ret.setSubjectCategory(xbat.getSubjectCategory());

        return ret;
    }

/*
    public SubjectAttributeDesignator getSubjectAttributeDesignator(ExpressionType xbet) throws Exception {
        if (xbet == null) return null;
        SubjectAttributeDesignatorType at = (SubjectAttributeDesignatorType) xbet;
        return getSubjectAttributeDesignator(at);
    }
*/

    public AttributeDesignator getAttributeDesignator(AttributeDesignator template
                                                      , AttributeDesignatorType xbat
                                                      , PolicyObject parent) throws Exception {
        if (xbat == null) return null;

        template.setAttributeId(xbat.getAttributeId());
        template.setDataType(xbat.getDataType());
        if (xbat.isSetIssuer()) template.setIssuer(xbat.getIssuer());
        if (xbat.isSetMustBePresent()) template.setMustBePresent(xbat.getMustBePresent());
        template.setParentPolicyObject(parent);

        return template;
    }

    public AttributeDesignator getAttributeDesignator(AttributeDesignatorType xbat, PolicyObject parent) throws Exception {
        if (xbat == null) return null;
        AttributeDesignator ret = null;
        if (xbat instanceof SubjectAttributeDesignatorType) {
            ret = new SubjectAttributeDesignatorImpl();
            return getSubjectAttributeDesignator((SubjectAttributeDesignatorType) xbat, parent);

        }

        String elName = xbat.newCursor().getName().getLocalPart();
        if ("ResourceAttributeDesignator".equals(elName)) {
            ret = new ResourceAttributeDesignatorImpl();
        } else if ("ActionAttributeDesignator".equals(elName)) {
            ret = new ActionAttributeDesignatorImpl();
        } else if ("EnvironmentAttributeDesignator".equals(elName)) {
            ret = new EnvironmentAttributeDesignatorImpl();
        }
        return getAttributeDesignator(ret, xbat, parent);
    }

}
